//
//  DisturbViewController.m
//  SelfDriving
//
//  Created by apple on 16/1/11.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "DisturbViewController.h"
#import "LoginResponseModel.h"
#import "BusinessService+User.h"
@interface DisturbViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *time;
@property (weak, nonatomic) IBOutlet UIPickerView *start_viw;
@property (weak, nonatomic) IBOutlet UIPickerView *end_viw;
@property (weak, nonatomic) IBOutlet UISwitch *swview;

@end

@implementation DisturbViewController
{
    NSMutableArray *sdata;
    NSMutableArray *edata;
    
    NSString *stime;
    NSString *etime;
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    stime = @"00:00";
    etime = @"00:00";
    sdata = [NSMutableArray arrayWithObjects:@"00:00",@"01:00",@"02:00",@"03:00",@"04:00",@"05:00",@"06:00",@"07:00",@"08:00",@"09:00",@"10:00",@"11:00",@"12:00",@"13:00",@"14:00",@"15:00",@"16:00",@"17:00",@"18:00",@"19:00",@"20:00",@"21:00",@"22:00",@"23:00", nil];
    edata = [NSMutableArray arrayWithObjects:@"00:00",@"01:00",@"02:00",@"03:00",@"04:00",@"05:00",@"06:00",@"07:00",@"08:00",@"09:00",@"10:00",@"11:00",@"12:00",@"13:00",@"14:00",@"15:00",@"16:00",@"17:00",@"18:00",@"19:00",@"20:00",@"21:00",@"22:00",@"23:00", nil];
    [[AppDelegate shareDelegate]refdata:^(id res) {
        if([[AppDelegate shareDelegate].user.wurao_start trim].length !=0){
            _swview.on = YES;
            stime = [AppDelegate shareDelegate].user.wurao_start;
            etime = [AppDelegate shareDelegate].user.wurao_end;
            _start_viw.hidden = NO;
            _end_viw.hidden = NO;
            _time.hidden = NO;
            _time.text = [NSString stringWithFormat:@"%@ to %@",stime,etime];
            for (int i=0;i<sdata.count;i++) {
                if ([[sdata objectAtIndex:i] isEqualToString:stime]) {
                    [_start_viw selectRow:i inComponent:0 animated:YES];
                }
            }
            for (int i = 0; i<edata.count; i++) {
                if ([[edata objectAtIndex:i] isEqualToString:etime]) {
                    [_end_viw selectRow:i inComponent:0 animated:YES];
                }
            }
        }else{
            _swview.on = NO;
        }
    }];
    _start_viw.delegate = self;
    _start_viw.dataSource = self;
    _end_viw.delegate = self;
    _end_viw.dataSource = self;
    _start_viw.tag = 100;
    _end_viw.tag = 101;
    
}
- (IBAction)switchActino:(UISwitch *)sender {
    if (!sender.on) {
        _start_viw.hidden = YES;
        _end_viw.hidden = YES;
        _time.hidden = YES;
    }else{
        _start_viw.hidden = NO;
        _end_viw.hidden = NO;
        _time.hidden = NO;
    }
    
    
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (pickerView.tag == 100) {
        return sdata.count;
    }
    return [edata count];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (pickerView.tag == 100) {
        return [sdata objectAtIndex:row];
    }else{
        return [edata objectAtIndex:row];
    }
    return nil;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (pickerView.tag == 100) {
        stime = [sdata objectAtIndex:row];
    }else{
        etime = [edata objectAtIndex:row];
    }
    _time.text = [NSString stringWithFormat:@"%@ to %@",stime,etime];
}
#pragma mark 保存
- (IBAction)submitAction:(UIBarButtonItem *)sender {
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:!_swview.on?@"0":@"1" forKey:@"type"];
    [dic setObject:stime forKey:@"wurao_start"];
    [dic setObject:etime forKey:@"wurao_end"];
    [[BusinessService shareInstance]setUserWurao:dic
                                         success:^(id result) {
                                             [SVProgressHUD showSuccessWithStatus:@"设置成功"];
                                         } failure:^(id result) {
                                             [SVProgressHUD showErrorWithStatus:result];
                                         }];
    
}
- (IBAction)backActino:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
